<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.json"};
  </script>

  <meta name="description" content="搬砖 嵌入式">
<meta property="og:type" content="website">
<meta property="og:title" content="学无止境">
<meta property="og:url" content="http://example.com/page/26/index.html">
<meta property="og:site_name" content="学无止境">
<meta property="og:description" content="搬砖 嵌入式">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="xudonglei">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/page/26/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false,
    lang   : 'zh-CN'
  };
</script>

  <title>学无止境</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">学无止境</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Do not bb, show me the example</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content index posts-expand">
            
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/31/%E6%95%B4%E6%95%B0%E5%B9%B3%E6%96%B9%E6%A0%B9%E5%BF%AB%E9%80%9F%E6%B1%82%E6%B3%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/03/31/%E6%95%B4%E6%95%B0%E5%B9%B3%E6%96%B9%E6%A0%B9%E5%BF%AB%E9%80%9F%E6%B1%82%E6%B3%95/" class="post-title-link" itemprop="url">整数平方根快速求法</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-03-31 16:36:01" itemprop="dateCreated datePublished" datetime="2021-03-31T16:36:01+08:00">2021-03-31</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F/" itemprop="url" rel="index"><span itemprop="name">嵌入式</span></a>
                </span>
            </span>

          
            <span id="/2021/03/31/%E6%95%B4%E6%95%B0%E5%B9%B3%E6%96%B9%E6%A0%B9%E5%BF%AB%E9%80%9F%E6%B1%82%E6%B3%95/" class="post-meta-item leancloud_visitors" data-flag-title="整数平方根快速求法" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/03/31/%E6%95%B4%E6%95%B0%E5%B9%B3%E6%96%B9%E6%A0%B9%E5%BF%AB%E9%80%9F%E6%B1%82%E6%B3%95/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/03/31/%E6%95%B4%E6%95%B0%E5%B9%B3%E6%96%B9%E6%A0%B9%E5%BF%AB%E9%80%9F%E6%B1%82%E6%B3%95/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>​    在嵌入式平台上做整数平方根，调用math库自带的sqrt函数会比较耗时，这边找到一种快速计算的方法，具体原理可以参考文档：<a target="_blank" rel="noopener" href="http://www.azillionmonkeys.com/qed/ulerysqroot.pdf">http://www.azillionmonkeys.com/qed/ulerysqroot.pdf</a></p>
<p>​    这边给出直接可以使用的代码，如下所示：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">mySqrt</span><span class="params">(<span class="keyword">int</span> v)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">unsigned</span> <span class="keyword">long</span> temp, nHat = <span class="number">0</span>, b = <span class="number">0x8000</span>, bshft = <span class="number">15</span>;</span><br><span class="line">    <span class="keyword">do</span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (v &gt;= (temp = (((nHat &lt;&lt; <span class="number">1</span>) + b) &lt;&lt; bshft--)))</span><br><span class="line">        &#123;</span><br><span class="line">            nHat += b;</span><br><span class="line">            v -= temp;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">while</span> (b &gt;&gt;= <span class="number">1</span>);</span><br><span class="line">    <span class="keyword">return</span> nHat;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>​    在stm32f429平台上写了测试代码做对比：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210331163241.png" style="zoom:70%;">

<p>​    调用math库自带的sqrt大概需要10us，使用上述方法，需要1.3us左右，效果比较明显。</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信图片_20210331162936.jpg" style="zoom:40%;">

<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信图片_20210331162938.jpg" style="zoom:40%;">
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/30/python%E5%9F%BA%E4%BA%8Escipy%E5%AE%9E%E7%8E%B0%E9%AB%98%E9%80%9A%E6%BB%A4%E6%B3%A2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/03/30/python%E5%9F%BA%E4%BA%8Escipy%E5%AE%9E%E7%8E%B0%E9%AB%98%E9%80%9A%E6%BB%A4%E6%B3%A2/" class="post-title-link" itemprop="url">python基于scipy实现高通滤波</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-03-30 18:06:30" itemprop="dateCreated datePublished" datetime="2021-03-30T18:06:30+08:00">2021-03-30</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/python/" itemprop="url" rel="index"><span itemprop="name">python</span></a>
                </span>
            </span>

          
            <span id="/2021/03/30/python%E5%9F%BA%E4%BA%8Escipy%E5%AE%9E%E7%8E%B0%E9%AB%98%E9%80%9A%E6%BB%A4%E6%B3%A2/" class="post-meta-item leancloud_visitors" data-flag-title="python基于scipy实现高通滤波" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/03/30/python%E5%9F%BA%E4%BA%8Escipy%E5%AE%9E%E7%8E%B0%E9%AB%98%E9%80%9A%E6%BB%A4%E6%B3%A2/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/03/30/python%E5%9F%BA%E4%BA%8Escipy%E5%AE%9E%E7%8E%B0%E9%AB%98%E9%80%9A%E6%BB%A4%E6%B3%A2/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>​    简单的理解就是低通滤波指的是去除高于某一阈值频率的信号；高通滤波去除低于某一频率的信号；带通滤波指的是类似低通高通的结合保留中间频率信号；带阻滤波也是低通高通的结合只是过滤掉的是中间部分。</p>
<p>​    以低通滤波为例，例如我们如果只是统计脉搏信号波形，应该在1Hz左右，却发现波形信号上有很多噪音，这些噪音都是成百上千Hz的，这些对于脉搏信号波形就属于无用的噪音，我们就可以通过低通滤波器将超出某一阈值的信号过滤掉，此时得到的波形就会比较平滑了。</p>
<p>​    下面直接用代码举高通滤波的例子：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/1234.png" style="zoom:60%;">

<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/Image1123.png" style="zoom:60%;">

<p>​    再简单介绍一下<strong>滤波器构造函数(仅介绍Butterworth滤波器)</strong></p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/Image111.png"></p>
<p>下面这个是自己使用signal.butter低通的例子，非常直观形象:</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E4%BD%8E%E9%80%9A%E7%9A%84%E6%95%88%E6%9E%9C.png"></p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/21/python%E6%93%8D%E4%BD%9Cjlink/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/03/21/python%E6%93%8D%E4%BD%9Cjlink/" class="post-title-link" itemprop="url">python操作jlink</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-03-21 13:20:16" itemprop="dateCreated datePublished" datetime="2021-03-21T13:20:16+08:00">2021-03-21</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F/" itemprop="url" rel="index"><span itemprop="name">嵌入式</span></a>
                </span>
            </span>

          
            <span id="/2021/03/21/python%E6%93%8D%E4%BD%9Cjlink/" class="post-meta-item leancloud_visitors" data-flag-title="python操作jlink" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/03/21/python%E6%93%8D%E4%BD%9Cjlink/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/03/21/python%E6%93%8D%E4%BD%9Cjlink/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>​    python界有一个神器：pylink-square，这个包可以用来控制jlink。它可以在MCU运行期间连上MCU，还不会打断MCU的运行，还可以把MCU的RAM，FLASH中的内容实时地读出来，读取的操作不会让MCU停下来。</p>
<h2 id="1-安装"><a href="#1-安装" class="headerlink" title="1.安装"></a>1.安装</h2><p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210314152240.png"></p>
<h2 id="2-找到jlink"><a href="#2-找到jlink" class="headerlink" title="2. 找到jlink"></a>2. 找到jlink</h2><p>​    pylink找到jlink的依据是这个jlink的序列号，可以用下面的代码找到这个序列号：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pylink</span><br><span class="line">jlink_dll_path = <span class="string">&#x27;./JLinkARM.dll&#x27;</span></span><br><span class="line">jlink = pylink.JLink(lib=pylink.library.Library(dllpath=jlink_dll_path))</span><br><span class="line">pjlist = jlink.connected_emulators()</span><br><span class="line">    <span class="keyword">for</span> item <span class="keyword">in</span> pjlist:</span><br><span class="line">        print(item.SerialNumber)</span><br></pre></td></tr></table></figure>
<p>​    pylink调用J-Link驱动JLinkARM.dll文件实现对指定J-Link的连接和控制，只需要一个JLinkARM.dll文件就能控制J-Link，无须另装驱动。很多使用ARM内核的芯片厂商自家的命令行烧录工具也是使用JLinkARM.dll来烧录。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#pylink中连接jlink</span></span><br><span class="line">jlink = pylink.JLink(lib=pylink.library.Library(dllpath=jlink_dll_path))</span><br><span class="line">jlink.<span class="built_in">open</span>(<span class="string">&#x27;123456&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h2 id="3-连接芯片"><a href="#3-连接芯片" class="headerlink" title="3.连接芯片"></a>3.连接芯片</h2><p>​    连接芯片之前，需要注意，pylink默认是用JTAG接口的，如果jlink跟芯片是用SWD连接的，要设置pylink的接口为SWD，接口选对之后，我们要告诉pylink连接的芯片是什么型号。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">jlink.set_tif(pylink.enums.JLinkInterfaces.SWD)</span><br><span class="line">jlink.connect(<span class="string">&#x27;STM32F103C8&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h2 id="4-提供的一些操作接口"><a href="#4-提供的一些操作接口" class="headerlink" title="4.提供的一些操作接口"></a>4.提供的一些操作接口</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">print(jlink.core_id())</span><br><span class="line">print(jlink.device_family())</span><br><span class="line">print(jlink.target_connected())</span><br><span class="line">jlink.flash_file(<span class="string">&#x27;F:/xxx.hex&#x27;</span>, <span class="number">0</span>)   <span class="comment"># 设置.hex路径  和下载的开始地址。</span></span><br><span class="line">jlink.memery_read8(addr,num)        <span class="comment"># 读FLASH，RAM中的数据，addr就是地址，num就是读的字节数，读成功的话，就会返回一个list，里面就是我们想要的数据，读出来的数据是小端模式。</span></span><br><span class="line">例子：</span><br><span class="line">x = jlink.memory_read8(<span class="number">0</span>,<span class="number">1024</span>)</span><br><span class="line">y = <span class="built_in">bytearray</span>(x)</span><br><span class="line">z = binascii.b2a_hex(y)</span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/17/esp32%20hello%20world%E5%B7%A5%E7%A8%8B%E5%88%86%E6%9E%90/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/03/17/esp32%20hello%20world%E5%B7%A5%E7%A8%8B%E5%88%86%E6%9E%90/" class="post-title-link" itemprop="url">esp32 hello world工程分析</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-03-17 09:16:53" itemprop="dateCreated datePublished" datetime="2021-03-17T09:16:53+08:00">2021-03-17</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F/" itemprop="url" rel="index"><span itemprop="name">嵌入式</span></a>
                </span>
            </span>

          
            <span id="/2021/03/17/esp32%20hello%20world%E5%B7%A5%E7%A8%8B%E5%88%86%E6%9E%90/" class="post-meta-item leancloud_visitors" data-flag-title="esp32 hello world工程分析" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/03/17/esp32%20hello%20world%E5%B7%A5%E7%A8%8B%E5%88%86%E6%9E%90/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/03/17/esp32%20hello%20world%E5%B7%A5%E7%A8%8B%E5%88%86%E6%9E%90/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="1-文件夹说明"><a href="#1-文件夹说明" class="headerlink" title="1.文件夹说明"></a>1.文件夹说明</h2><p>​    esp32的hello world工程的必须文件除了.c文件之外就只有两个CMakeLists.txt文件，如下图所示。</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210316204804.png" style="zoom:67%;">

<p>编译的话会生成build文件夹和sdkconfig文件，build文件夹里是编译产生的过程文件和最终的bin文件，sdkconfig是用来存一些设置的。sdkconfig这个文件可以删除也可以不删掉，删除的话sdkconfig所有都会按默认设置生成，hello world这个例子很简单所以你可以删掉。</p>
<h2 id="2-cmake语句介绍"><a href="#2-cmake语句介绍" class="headerlink" title="2.cmake语句介绍"></a>2.cmake语句介绍</h2><p>​    espidf在4.0之后使用cmake来建立工程，下面简单介绍一下。</p>
<h3 id="2-1-外层CMakeLists-txt"><a href="#2-1-外层CMakeLists-txt" class="headerlink" title="2.1 外层CMakeLists.txt"></a>2.1 外层CMakeLists.txt</h3><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210316210042.png" style="zoom:50%;">

<p>​    第五行include表示要导入espidf里的一个cmake文件,这里的文件作用是把espidf里面所有的库放到工程里，所以我们才能调用espidf这里的函数。</p>
<p>​    第六行project是命名工程名。</p>
<h2 id="2-2-内层CMakeLists-txt"><a href="#2-2-内层CMakeLists-txt" class="headerlink" title="2.2 内层CMakeLists.txt"></a>2.2 内层CMakeLists.txt</h2><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210316210534.png" style="zoom:67%;">

<p>​    这里面就一个函数，这个函数来自外层cmake导入的文件，我们通过这个函数把自己的文件加入工程。这个函数的SRCS就是添加.c文件，而INCLUDE_DIRS用来添加.h文件所在的文件夹。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">idf_component_register():      idf文件注册，是idf特有的</span><br><span class="line"> 参数：SRCS:源文件(c文件)</span><br><span class="line"> 参数：INCLUDE_DIRS:头文件夹</span><br><span class="line"> 参数：REQUIRES:依赖(依赖的库)</span><br></pre></td></tr></table></figure>
<p>如果外面我们的.c文件改了，这里CMakeLists.txt文件也要修改，为了方便起见，可以改成下面这样的形式：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210316211642.png" style="zoom:67%;">

<p>之后我们就可以安心添加我们的源文件和头文件了，可以安心写应用了。</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/14/%E7%94%A8anaconda%E4%BD%BF%E5%BE%9764%E4%BD%8D%E5%92%8C32%E4%BD%8Dpython%E5%85%B1%E5%AD%98/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/03/14/%E7%94%A8anaconda%E4%BD%BF%E5%BE%9764%E4%BD%8D%E5%92%8C32%E4%BD%8Dpython%E5%85%B1%E5%AD%98/" class="post-title-link" itemprop="url">用anaconda使得64位和32位python共存</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-03-14 17:52:06" itemprop="dateCreated datePublished" datetime="2021-03-14T17:52:06+08:00">2021-03-14</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/python/" itemprop="url" rel="index"><span itemprop="name">python</span></a>
                </span>
            </span>

          
            <span id="/2021/03/14/%E7%94%A8anaconda%E4%BD%BF%E5%BE%9764%E4%BD%8D%E5%92%8C32%E4%BD%8Dpython%E5%85%B1%E5%AD%98/" class="post-meta-item leancloud_visitors" data-flag-title="用anaconda使得64位和32位python共存" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/03/14/%E7%94%A8anaconda%E4%BD%BF%E5%BE%9764%E4%BD%8D%E5%92%8C32%E4%BD%8Dpython%E5%85%B1%E5%AD%98/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/03/14/%E7%94%A8anaconda%E4%BD%BF%E5%BE%9764%E4%BD%8D%E5%92%8C32%E4%BD%8Dpython%E5%85%B1%E5%AD%98/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>​    Python中调用JLinkARM.dll出现OSError: [WinError 193] %1 不是有效的 Win32 应用程序，是因为python位数和dll位数不一样导致的，dll是32位的，而python是64位的(通过conda info命令可以看到当前platform：win-64)。解决方法推荐安装32位的python，用anaconda保证64位和32位的python共存。</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#为32位Python 2.7创建一个新的环境：</span></span><br><span class="line"><span class="built_in">set</span> CONDA_FORCE_32BIT=1</span><br><span class="line">conda create -n py27_32 python=2.7</span><br><span class="line"></span><br><span class="line"><span class="comment">#激活它：</span></span><br><span class="line"><span class="built_in">set</span> CONDA_FORCE_32BIT=1</span><br><span class="line">activate py27_32</span><br><span class="line"></span><br><span class="line"><span class="comment">#停用它：</span></span><br><span class="line">deactivate py27_32</span><br></pre></td></tr></table></figure>
<p>​    激活后你再执行conda info，可以发现已经变成了win-32。</p>
<p>​    然后你在anaconda中python的解释器环境选择py27_32就可以了：</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210314174450.png"></p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/14/Usbser.sys%E9%A9%B1%E5%8A%A8%E7%A8%8B%E5%BA%8F%E5%92%8CINF%E6%96%87%E4%BB%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/03/14/Usbser.sys%E9%A9%B1%E5%8A%A8%E7%A8%8B%E5%BA%8F%E5%92%8CINF%E6%96%87%E4%BB%B6/" class="post-title-link" itemprop="url">Usbser.sys驱动程序和INF文件</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-03-14 15:06:06" itemprop="dateCreated datePublished" datetime="2021-03-14T15:06:06+08:00">2021-03-14</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/USB/" itemprop="url" rel="index"><span itemprop="name">USB</span></a>
                </span>
            </span>

          
            <span id="/2021/03/14/Usbser.sys%E9%A9%B1%E5%8A%A8%E7%A8%8B%E5%BA%8F%E5%92%8CINF%E6%96%87%E4%BB%B6/" class="post-meta-item leancloud_visitors" data-flag-title="Usbser.sys驱动程序和INF文件" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/03/14/Usbser.sys%E9%A9%B1%E5%8A%A8%E7%A8%8B%E5%BA%8F%E5%92%8CINF%E6%96%87%E4%BB%B6/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/03/14/Usbser.sys%E9%A9%B1%E5%8A%A8%E7%A8%8B%E5%BA%8F%E5%92%8CINF%E6%96%87%E4%BB%B6/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>​    最近在看一个TeenyUSB的开源项目，这个项目里面作者自己做了一个上位机工具来生成USB驱动，为了实现这个功能作者借助了另外一个开源项目：libwdi。</p>
<h2 id="1-Usbser-sys驱动程序"><a href="#1-Usbser-sys驱动程序" class="headerlink" title="1. Usbser.sys驱动程序"></a>1. Usbser.sys驱动程序</h2><p>​    TeenyUSB中可以使用多种驱动，比如WinUSB，Usbser，libusb等，这里简单介绍一下Usbser.sys。它是USB 串行驱动程序，是windows自带的 ，支持Windows 10和Windows 8.1，用于通信和 CDC 控制设备的 Microsoft 提供的内置驱动程序。</p>
<h2 id="2-INF文件"><a href="#2-INF文件" class="headerlink" title="2. INF文件"></a>2. INF文件</h2><p>​    通用串行总线 (USB) 调制解调器 .inf 文件可以使用 Usbser.sys 驱动程序，libwdi开源项目提供了这个inf文件的模板，我们只要在它的基础上修改几处就可以为我们自己所用。看下面TeenyUSB中其中一个例子，如下图所示：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210201183310.png" style="zoom:60%;">

<p>左侧是libwdi中提供的模板，右边是上位机工具实际生成的一个inf文件。</p>
<p>​    这个是在模板的基础上改的，如果没有模板就需要我们自己去写，那就需要了解INF文件，知道它的一些规则，下面就简单介绍USB驱动的INF文件。</p>
<p>​    INF文件向操作系统提供了安装驱动所需的信息，当一个新设备插入PC后，操作系统根据提供的INF文件自动完成驱动程序的安装。Inf是Device information File的缩写，是Microsoft公司为硬件设备制造商发布其驱动程序推出的一种文件格式，Inf文件中包含硬件设备的信息或脚本以控制硬件操作。在Inf文件中指明了硬件驱动该如何安装到系统中，源文件在哪里、安装到哪一个文件夹中、怎样在注册表中加入自身相关信息等。</p>
<p><strong>Inf文件格式：</strong></p>
<ol>
<li>每个inf文件由许多节（section）组成，节名用方括号括起来。这些节名有些是系统定义好的，有一些是用户自定义的。每一个节名最长为255个字符，节与节之间没有先后顺序。节下面有一个或多个字段（或者叫条目line，也有叫指令directive），如果同一个Inf文件中出现两个一样的节名，则系统会自动将这两个节名下面的条目合并到一起。</li>
<li>在节与节之间的内容叫条目或项，每一个节又是由许多的条目组成，每个条目都是由形如”signature=”$WINDOWS NT$””的形式。如果每一个条目的等号后面有多个值，则每一个值之间用“，”号分隔开。</li>
<li>Inf文件对大小写不敏感。</li>
<li>“；”后面的内容为注释。</li>
<li> 如果一个条目的内容过多，在一行无法书写完全，则用“\”将一行内容书写多行。</li>
</ol>
<p><strong>关键和常用的节（section）</strong>：</p>
<ul>
<li><p>[Version]版本描述信息，主要用于版本控制，是唯一必须的节。</p>
</li>
<li><p>[Strings]字符串信息，用于常量定义，即其它节要用到的字符串常量都可以保存到这里</p>
</li>
<li><p>[DestinationDirs]定义系统路径信息，与Copy Files、Rename Files、Delete Files等节配合，用于指定其缺省操作目录。</p>
</li>
<li><p>[SourceDisksNames]要复制或安装的文件位置。</p>
</li>
<li><p>[SourceDisksFiles]要复制或安装的文件名。</p>
</li>
<li><p>[Manufacturer]指明供应商及其对应Models节的名称</p>
</li>
<li><p>[Models]指明Install/DDInstall节的名称、设备的硬件ID等信息（注意，Models的名称由Manufacturer节定义）</p>
</li>
<li><p>[Install]指明需复制的文件、想注册表中添加的内容等信息</p>
</li>
<li><p>[DDInstall.Services]指明驱动程序安装的详细信息 </p>
</li>
</ul>
<p>具体INF文件中各节和其条目的详细说明这边就不展开了，可以直接上网查阅到。</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/14/Jtag%20TAP%E7%8A%B6%E6%80%81%E6%9C%BA%E7%AE%80%E4%BB%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/03/14/Jtag%20TAP%E7%8A%B6%E6%80%81%E6%9C%BA%E7%AE%80%E4%BB%8B/" class="post-title-link" itemprop="url">Jtag TAP状态机简介</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-03-14 11:15:05" itemprop="dateCreated datePublished" datetime="2021-03-14T11:15:05+08:00">2021-03-14</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F/" itemprop="url" rel="index"><span itemprop="name">嵌入式</span></a>
                </span>
            </span>

          
            <span id="/2021/03/14/Jtag%20TAP%E7%8A%B6%E6%80%81%E6%9C%BA%E7%AE%80%E4%BB%8B/" class="post-meta-item leancloud_visitors" data-flag-title="Jtag TAP状态机简介" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/03/14/Jtag%20TAP%E7%8A%B6%E6%80%81%E6%9C%BA%E7%AE%80%E4%BB%8B/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/03/14/Jtag%20TAP%E7%8A%B6%E6%80%81%E6%9C%BA%E7%AE%80%E4%BB%8B/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>​    这篇博文”参考”(复制)了网上很多网友的资料，有gitee上<a target="_blank" rel="noopener" href="https://gitee.com/liangkangnan"> liangkangnan</a>，阿莫论坛上<a target="_blank" rel="noopener" href="https://www.amobbs.com/space-uid-261204.html">Gorgon_Meducer</a>，以及其他一些还叫不上名字的各位大神，在这表示对他们的感谢，有些是直接从他们的博文中复制粘贴下来的，感谢感谢！！！</p>
<p>​    Jtag协议是目前应用最广泛的下载和仿真协议，对于协议的缔造者来说，也许有点无心插柳柳成荫的感觉，因为最初的jtag协议只是用来辅助专门的硬件质检部门对印刷电路进行检测的，但是现在看到的各种JTAG下载和仿真协议都是各生产厂商在原有JTAG协议的思想上进行扩充的，并不具有通用性，甚至连最基本的电路检测部分功能也被加以改造。虽然很多东西都改了，但不变的部分就只有其核心TAP状态机。</p>
<h2 id="1-Jtag简介"><a href="#1-Jtag简介" class="headerlink" title="1.Jtag简介"></a>1.Jtag简介</h2><p>​    标准JTAG采用四线方式，分别是TCK、TMS、TDI和TDO，有一个可选的TRST引脚。</p>
<ul>
<li>TCK：测试时钟输入。</li>
<li>TMS：测试模式选择。</li>
<li>TDI：测试数据输入。</li>
<li>TDO：测试数据输出。</li>
</ul>
<p>在调试时需要用到一个工具，比如JLink或者CMSIS-DAP，对于这个工具，在这里称为JTAG主机(JTAG host)，而嵌入在芯片内部的JTAG称为JTAG从机(JTAG slave)，需要注意的是上面这些信号的输入输出方向是对于JTAG从机来说的。</p>
<p>​    一个JTAG主机可以同时对多个JTAG从机进行调试，这通过JTAG扫描链(JTAG Scan Chain)完成，如下图所示。</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210314104442.png" style="zoom:50%;">

<h2 id="2-原理解析"><a href="#2-原理解析" class="headerlink" title="2.原理解析"></a>2.原理解析</h2><p>​    在被复杂的时序图弄得眼花缭乱之前，你应该首先记住以下的断言：<br>  A、JTAG协议的本质与SPI协议并没有什么不同，它等于一个复杂的SS状态机+变长的MOSI和MISO数据移位<br>​    操作。不过所谓的变长，都是事先约定好的。<br>  B、JTAG协议是一个同步通讯协议，它是全双工的。它的通讯原则是“以物易物”——即你如果想得到某<br>​    些东西，你必须先给与相同长度的内容；你如果只是想发送一些数据，也会自动获取相同长度的内容，<br>​    至于交换的内容是否有意义，这是另外一回事了。<br>  C、JTAG协议无论多么复杂，实际上只有4根线起作用（有时候还有两根鸡肋的nSRST和TRST），他们分别<br>​    是TMS、TCK、TDI和TDO，他们分别对应SPI协议里面的SS、SCK、MOSI和MISO。在本质上，他们并没有<br>​    什么不同。即便是ARM的JTAG那么多的引脚，实际上起作用JTAG的也就这4根线而已。<br>  D、JTAG的数据操作都是基于移位寄存器的。<br>  E、如果JTAG协议在某个下载仿真协议中只是用来发送控制信息和少量的数据，而大量的数据传输是通过<br>​    额外的其它引脚进行的，即便这个协议被称为JTAG仿真其本质也早已超过JTAG了，严格来说，不应该<br>​    称之为JTAG。因为JTAG协议中就只有4根线（有时候也算上nSRST和TRST）而已。</p>
<h2 id="3-TAP状态机"><a href="#3-TAP状态机" class="headerlink" title="3.TAP状态机"></a>3.TAP状态机</h2><p>​    如果熟悉SPI，你会发现SPI是如此的简单、和谐——一根用于二进制位同步的时钟线SCK，一根用于主机发送到从机的数据线MOSI（Master Out Slave In），一根用于从机发送到主机的数据线MISO（Master In Slave Out），外加一根用于复位SPI从机控制器以达到通讯同步的控制信号SS（不妨理解为Slave Synchronization）。如果乐意，你很容易就通过消减MOSI和MISO的方法获得精简的单向通讯。<br>​     前面我们提到，JTAG的数据操作都是基于移位寄存器的，这一点和SPI是相同的。JTAG协议的核心在于能够有效地传输指令和数据，并且传输指令和数据的过程是能够明确的加以区别对待而不会造成歧义的。如何实现这一功能呢？JTAG协议只是将原本用来复位从机的SS信号扩展为一个状态机。JTAG内部有一个TAP(Test Access Port)控制器(或者说状态机)，通过TCK和TMS信号来改变状态机的状态。这个状态机的核心是两路SCAN，分别是IR SCAN和DR SCAN，TAP状态机如下图所示。</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/TAP.png" style="zoom:25%;">

<p>​    箭头上的0或1表示的是TMS信号的电平。JTAG在每一个TCK信号的上升沿采样TMS信号和TDI信号，决定状态机的状态是否发生变化，在每一个TCK信号的下降沿输出TDO信号。可以看到，无论TAP目前处于哪一个状态，只要TMS保持高电平并持续5个TCK时钟，则TAP一定会回到Test-Logic-Reset状态。值得庆幸的是，该状态机所有JTAG协议中都会遵守的部分。</p>
<p>​    其实整个状态机不过分为三个部分：信道选择部分、数据信道和指令信道。</p>
<p>​    所谓的信道选择，就是图中最顶上由四个状态组成的矩形，分别对应着四个状态：<br>​    1、JTAG TAP状态机复位状态<br>​    顾名思义，就是进入该状态，将导致整个硬件TAP控制器复位，所有的寄存器都将被初始化。在TCK的上升沿，TMS为低电平时，进入下一个状态；否则保持不变。<br>​    2、JTAG TAP的Run-Test/Idle状态<br>​    其实就是“开工”和“休息”的选择分支点。在TCK的上升沿，TMS的高电平将导致状态切换，进入数据信道的通讯状态；否则保持不变。<br>​    3、JTAG TAP的Select-DR Scan状态<br>​    Select DR Scan翻译成中文就是“选择数据移位寄存器进行移位操作”，简单说来，就是当我们在该状态下，TCK的上升沿读取到了TMS的低电平将直接进入数据信道的操作子状态机；在TCK的上升沿读取到了TMS的高电平，将切换到指令信道的通讯状态。<br>​    4、JTAG TAP的Select-IR Scan状态<br>​    Select-IR Scan翻译成中文就是“选择指令寄存器进行移位操作”，简单来说，就是当我们在该状态下，TCK的上升沿读取到了TMS的低电平将直接进入指令信道的操作状态机；在TCK的上升沿读取到了TMS的高电平，将重新回到JTAG的复位状态。</p>
<p>​    下面，就常见的几个状态进行解释（以数据信道为例，指令信道可以参考其内容）。</p>
<p>1、Capture DR状态<br>     前文说过，JTAG协议是基于移位寄存器的，其通讯具有“以物易物”的特性，在我们进入真正的数据传输之前，需要告知JTAG“准备通讯了哦？你有没有东西要给我哈？”，于是Capture DR就是一个给JTAG机会将需要传达给我们的数据放入指定的移位寄存器中的状态。<br>2、Shift DR状态<br>     这个状态就是通过TDI和TDO进行数据传输的状态。需要说明的是，即便进入了该状态，TMS上的电平在TCK的上升沿也是会被读取的，从图中看到，一旦在TMS上读取到高电平，系统就会跳出Shift DR状态如果此时数据没有传输完成，造成的后果是不确定的。请大家注意，我所说的是不确定，而不是“很严重”：同样是因为移位寄存的传输特性，有时候并不要求一定要将所有的数据都完整的进行传输，但有的时候，数据的不完整传输则会导致很严重的后果，这取决于具体的JTAG通讯协议。所以，为了保险起见，一旦进入Shift DR状态，在发送最后一个数据之前，请保持TMS为低电平，当要发送最后一个数据时，应该将TMS设置为高电平，这样，当TCK跳变为上升沿时，系统既完成了最后一个数据的传输，也成功的退出了Shift DR状态。<br>3、Exit1 DR状态<br>      该状态提供了我们一个在刚才输入的数据生效前，重新修改的机会。一般情况下，我们直接保持TMS的高电平，并在TCK的上升沿驱动TAP状态机，直接进入Update-DR状态。<br>4、Update-DR状态<br>     顾名思义，就是使我们输入的数据生效——一般JTAG内部的动作就是触发一个锁存信号，将移位寄存器中的内容并行的读取到对应的寄存器中。Update-DR有两个出口，一个是，TMS的低电平对应Run-test/Idle，还有一个是TMS的高电平对应的Select-DR Scan。这两个操作看似区别不大，但是意义非凡。前者往往会导致JTAG内部产生额外的时序（比如发生一个信号，表示完成了一个特定的周期操作，在AVR的JTAG下载中有此实例）；后者则表示完成了一次数据操作，将进行下一个数据的操作，但是这些操作属于同一个操作周期。当然有些情况下，这两种方法是没有区别的。</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/10/OpenOCD%E5%BC%80%E6%BA%90%E8%B0%83%E8%AF%95%E5%99%A8%E5%9C%A8windows%E4%B8%8A%E4%BD%BF%E7%94%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/03/10/OpenOCD%E5%BC%80%E6%BA%90%E8%B0%83%E8%AF%95%E5%99%A8%E5%9C%A8windows%E4%B8%8A%E4%BD%BF%E7%94%A8/" class="post-title-link" itemprop="url">OpenOCD开源调试器在windows上使用</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-03-10 11:09:07" itemprop="dateCreated datePublished" datetime="2021-03-10T11:09:07+08:00">2021-03-10</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F/" itemprop="url" rel="index"><span itemprop="name">嵌入式</span></a>
                </span>
            </span>

          
            <span id="/2021/03/10/OpenOCD%E5%BC%80%E6%BA%90%E8%B0%83%E8%AF%95%E5%99%A8%E5%9C%A8windows%E4%B8%8A%E4%BD%BF%E7%94%A8/" class="post-meta-item leancloud_visitors" data-flag-title="OpenOCD开源调试器在windows上使用" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/03/10/OpenOCD%E5%BC%80%E6%BA%90%E8%B0%83%E8%AF%95%E5%99%A8%E5%9C%A8windows%E4%B8%8A%E4%BD%BF%E7%94%A8/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/03/10/OpenOCD%E5%BC%80%E6%BA%90%E8%B0%83%E8%AF%95%E5%99%A8%E5%9C%A8windows%E4%B8%8A%E4%BD%BF%E7%94%A8/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="1-简介"><a href="#1-简介" class="headerlink" title="1.简介"></a>1.简介</h2><p>​    OpenOCD（Open On-Chip Debugger）是一个开源的片上调试器，旨在提供针对嵌入式设备的调试、系统编程和边界扫描功能。它的下载地址为：<a target="_blank" rel="noopener" href="http://www.gnutoolchains.com/arm-eabi/openocd/">http://www.gnutoolchains.com/arm-eabi/openocd/</a> ，其可调试数百种目标芯片，包括arm, mips, dsp, fpga, cpld等。支持多种调试接口，如cmsis-dap, jlink, stlink, usb-blaster等。</p>
<p>​    下载解压后目录如下图所示：</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210310092130.png"></p>
<p>其中目录OpenOCD-20210301-0.10.0\share\openocd\scripts\target下面是一些目标芯片平台的脚本，以stm32f1x.cfg这个文件为例。它是一个TCL的一个脚本实现，它里面是支持一些变量的设置，以及if else的这些逻辑判断。</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210310094219.png" style="zoom:67%;">

<p>​    这个文件中前面全都是在设置一些变量，最关键的是第46和47行，这两行配置目标芯片平台的一些属性，比如说芯片名字，芯片ID（比如你用的是swd的，那就是走的42行那个分支，芯片ID就是0x1ba01477；如果用的jtag，那就是走的39行那个分支）</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210310095055.png"></p>
<p>​    另外调试器可能会需要产生一些数据，这些数据要放在芯片内部的SRAM里面，上面图中56行就是配置它放在哪个地址，可以看到这边就是配置在芯片的一个SRAM的一个空间基地址。</p>
<h2 id="2-使用例子"><a href="#2-使用例子" class="headerlink" title="2. 使用例子"></a>2. 使用例子</h2><h3 id="2-1-attach"><a href="#2-1-attach" class="headerlink" title="2.1 attach"></a>2.1 attach</h3><p>​    attach到目标芯片上，观察输出，以确认仿真器和是否和目标板正确连接</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210310102615.png"></p>
<p>用jlink-ob一直没成功，换成cmsis-dap就没问题，暂时没去管其中的原因。</p>
<h3 id="2-2-擦除"><a href="#2-2-擦除" class="headerlink" title="2.2 擦除"></a>2.2 擦除</h3><p>​    擦除目标芯片flash</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210310103838.png"></p>
<h3 id="2-3-读"><a href="#2-3-读" class="headerlink" title="2.3 读"></a>2.3 读</h3><p>​    dump出目标芯片中的flash</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210310104341.png"></p>
<p>这就在openocd.exe文件所在的目录生成flash_image_dump.bin文件。</p>
<h3 id="2-4-写"><a href="#2-4-写" class="headerlink" title="2.4 写"></a>2.4 写</h3><p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210310105942.png"></p>
<h3 id="2-5-锁定"><a href="#2-5-锁定" class="headerlink" title="2.5 锁定"></a>2.5 锁定</h3><p>​    对目标芯片锁定，即增加读保护，加上读保护之后，无法从调试接口访问flash空间，若执行解锁操作，则芯片内部逻辑会自动擦除所有flash数据。</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210310110357.png"></p>
<h3 id="2-6-解锁"><a href="#2-6-解锁" class="headerlink" title="2.6 解锁"></a>2.6 解锁</h3><p>​    解锁目标芯片，会擦除所有flash数据。</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210310110820.png"></p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/09/%E5%88%86%E6%95%A3%E5%8A%A0%E8%BD%BD%E6%96%87%E4%BB%B6%E4%BC%98%E5%85%88%E7%94%A8%E5%86%85%E9%83%A8SRAM%E5%B9%B6%E6%8A%8A%E5%A0%86%E5%8C%BA%E5%88%86%E9%85%8D%E5%88%B0%20SDRAM/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/03/09/%E5%88%86%E6%95%A3%E5%8A%A0%E8%BD%BD%E6%96%87%E4%BB%B6%E4%BC%98%E5%85%88%E7%94%A8%E5%86%85%E9%83%A8SRAM%E5%B9%B6%E6%8A%8A%E5%A0%86%E5%8C%BA%E5%88%86%E9%85%8D%E5%88%B0%20SDRAM/" class="post-title-link" itemprop="url">分散加载文件优先用内部SRAM并把堆区分配到SDRAM</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-03-09 20:59:06" itemprop="dateCreated datePublished" datetime="2021-03-09T20:59:06+08:00">2021-03-09</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F/" itemprop="url" rel="index"><span itemprop="name">嵌入式</span></a>
                </span>
            </span>

          
            <span id="/2021/03/09/%E5%88%86%E6%95%A3%E5%8A%A0%E8%BD%BD%E6%96%87%E4%BB%B6%E4%BC%98%E5%85%88%E7%94%A8%E5%86%85%E9%83%A8SRAM%E5%B9%B6%E6%8A%8A%E5%A0%86%E5%8C%BA%E5%88%86%E9%85%8D%E5%88%B0%20SDRAM/" class="post-meta-item leancloud_visitors" data-flag-title="分散加载文件优先用内部SRAM并把堆区分配到SDRAM" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/03/09/%E5%88%86%E6%95%A3%E5%8A%A0%E8%BD%BD%E6%96%87%E4%BB%B6%E4%BC%98%E5%85%88%E7%94%A8%E5%86%85%E9%83%A8SRAM%E5%B9%B6%E6%8A%8A%E5%A0%86%E5%8C%BA%E5%88%86%E9%85%8D%E5%88%B0%20SDRAM/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/03/09/%E5%88%86%E6%95%A3%E5%8A%A0%E8%BD%BD%E6%96%87%E4%BB%B6%E4%BC%98%E5%85%88%E7%94%A8%E5%86%85%E9%83%A8SRAM%E5%B9%B6%E6%8A%8A%E5%A0%86%E5%8C%BA%E5%88%86%E9%85%8D%E5%88%B0%20SDRAM/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>​    这边使用了野火的教程（在这对野火表示感谢）：使得默认情况下优先使用内部 SRAM 空间，在需要的时候使用一个关键字指定变量存储到外部 SDRAM，另外，再把系统默认的堆空间(HEAP)映射到外部 SDRAM，从而可以使用malloc函数动态从SDRAM 中分配空间。</p>
<h2 id="1-在-main之前初始化SDRAM"><a href="#1-在-main之前初始化SDRAM" class="headerlink" title="1. 在__main之前初始化SDRAM"></a>1. 在__main之前初始化SDRAM</h2><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210308213446.png" style="zoom:50%;">

<h2 id="2-修改sct文件"><a href="#2-修改sct文件" class="headerlink" title="2. 修改sct文件"></a>2. 修改sct文件</h2><p>​    在sct文件中增加外部 SDRAM 空间对应的执行域：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210308224219.png" style="zoom:50%;">

<p>​    sct文件中对内部SRAM的执行域保留了默认配置，没有作任何改动，新增了一个外部SDRAM的执行域，并且使用了“*.o(HEAP)”语句把堆区分配到了 SDRAM 空间，使用“.ANY(EXRAM)”语句把名为“EXRAM”的节区分配到 SDRAM 空间。</p>
<h2 id="3-指定变量分配到节区"><a href="#3-指定变量分配到节区" class="headerlink" title="3. 指定变量分配到节区"></a>3. 指定变量分配到节区</h2><p>​    当我们需要把变量分配到外部 SDRAM 时，需要使用__attribute__关键字指定节区：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210308224959.png" style="zoom:67%;">

<p>​    “_attribute__ ((section (“节区名”)))”描述它要分配到的节区。本例中的节区名为“EXRAM”，即我们在sct 文件中选择分配到SDRAM执行域的节区，所以该变量就被分配到SDRAM中了。</p>
<p>​    根据我们sct文件的配置，如果定义变量时没有指定节区，它会默认优先使用内部SRAM，把变量定义到内部SRAM空间。<strong>而且由于局部变量是属于栈节区(STACK)，它不能使用“_attribute__”关键字指定节区，在本例中的栈节区被分配到内部SRAM空间</strong>。</p>
<h2 id="4-测试程序"><a href="#4-测试程序" class="headerlink" title="4. 测试程序"></a>4. 测试程序</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//设置变量定义到“EXRAM”节区的宏</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> __EXRAM __attribute__ ((section (<span class="meta-string">&quot;EXRAM&quot;</span>)))</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">//定义变量到 SDRAM</span></span><br><span class="line"><span class="keyword">uint32_t</span> testValue __EXRAM =<span class="number">7</span>;</span><br><span class="line"><span class="comment">//定义变量到 SRAM</span></span><br><span class="line"><span class="keyword">uint32_t</span> testValue2 =<span class="number">7</span>;</span><br><span class="line"><span class="comment">//定义数组到 SDRAM</span></span><br><span class="line"><span class="keyword">uint8_t</span> testGrup[<span class="number">3</span>] __EXRAM =&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;;</span><br><span class="line"><span class="comment">//定义数组到 SRAM</span></span><br><span class="line"><span class="keyword">uint8_t</span> testGrup2[<span class="number">3</span>] =&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123; </span><br><span class="line">	<span class="keyword">uint32_t</span> inerTestValue =<span class="number">10</span>;</span><br><span class="line">	delay_init(<span class="number">168</span>);		 </span><br><span class="line">	LED_Init();		      </span><br><span class="line">	uart_init(<span class="number">115200</span>);</span><br><span class="line">  </span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;地址：0x%x,  值为：%d\r\n&quot;</span>,(<span class="keyword">uint32_t</span>)&amp;inerTestValue,inerTestValue);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;地址：0x%x,  值为：%d\r\n&quot;</span>,(<span class="keyword">uint32_t</span>)&amp;testValue,testValue);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;地址：0x%x,  值为：%d\r\n&quot;</span>,(<span class="keyword">uint32_t</span>)&amp;testValue2,testValue2);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;地址：0x%x,  值为：%d,%d,%d\r\n&quot;</span>,(<span class="keyword">uint32_t</span>)&amp;testGrup,testGrup[<span class="number">0</span>],testGrup[<span class="number">1</span>],testGrup[<span class="number">2</span>]);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;地址：0x%x,  值为：%d，%d,%d\r\n&quot;</span>,(<span class="keyword">uint32_t</span>)&amp;testGrup2,testGrup2[<span class="number">0</span>],testGrup2[<span class="number">1</span>],testGrup2[<span class="number">2</span>]);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">uint32_t</span> * pointer = (<span class="keyword">uint32_t</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">uint32_t</span>)*<span class="number">3</span>);</span><br><span class="line">	<span class="keyword">if</span>(pointer != <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		*(pointer)=<span class="number">1</span>;</span><br><span class="line">        *(++pointer)=<span class="number">2</span>;</span><br><span class="line">        *(++pointer)=<span class="number">3</span>; </span><br><span class="line"></span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;地址为：0x%x\r\n&quot;</span>,(<span class="keyword">uint32_t</span>)pointer); </span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;*(pointer--)=%d, \r\n&quot;</span>,*(pointer--));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;*(pointer--)=%d, \r\n&quot;</span>,*(pointer--));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;*(pointer)=%d, \r\n&quot;</span>,*(pointer));</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">while</span>(<span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		GPIO_SetBits(GPIOF , GPIO_Pin_9);</span><br><span class="line">		delay_ms(<span class="number">1000</span>);</span><br><span class="line">		GPIO_ResetBits(GPIOF , GPIO_Pin_9);</span><br><span class="line">		delay_ms(<span class="number">1000</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>​    查看工程的map文件观察变量的分配情况:</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210309194605.png" style="zoom:80%;">

<p>​    从 map 文件中可看到普通变量及栈节区都被分配到了内部 SRAM 的地址区域，而指定到 EXRAM 节区的变量及堆空间都被分配到了外部 SDRAM 的地址区域，与我们的要求一致。再看串口打印出来的信息如下图所示：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210309195141.png" style="zoom:67%;">

<h2 id="5-使用malloc和free管理SDRAM的空间"><a href="#5-使用malloc和free管理SDRAM的空间" class="headerlink" title="5. 使用malloc和free管理SDRAM的空间"></a>5. 使用malloc和free管理SDRAM的空间</h2><p>​    SDRAM的内存空间非常大，为了管理这些空间，一些工程师会自己定义内存分配函数来管理SDRAM空间，这些分配过程本质上就是从SDRAM中动态分配内存。我们完全可以直接使用malloc从SDRAM中分配空间，只要在前面配置的基础上修改启动文件中的堆顶地址限制即可。</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210309203307.png" style="zoom:80%;">

<p>​    malloc函数是根据_heap_base及_heap_limit 地址限制分配空间的，在以上的代码定义中，堆基地址_heap_base 的由链接器自动分配未使用的基地址，而堆顶地址_heap_limit 则被定义为外部SDRAM的最高地址 0x68000000+0x00100000(使用这种定义方式定义的__heap_limit 值与 Heap_Size 定义的大小无关)，经过这样配置之后，SDRAM内除 EXRAM 节区外的空间都被分配为堆区，所以malloc函数可以管理剩余的所有 SDRAM空间。修改后，它生成的 map文件信息如下图所示：</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210309204823.png"></p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210309205449.png"></p>
<p>​    可看到_heap_base 的地址紧跟在 EXRAM 之后，__heap_limit 指向了 SDRAM 的最高</p>
<p>地址，因此 malloc 函数就能利用所有 SDRAM 的剩余空间了。注意图中显示的 HEAP 节区大小为 0x00000200 字节，修改启动文件中的 Heap_Size 大小可以改变该值，<strong>它的大小是不会影响 malloc 函数使用的</strong>，malloc 实际可用的就是_heap_base 与__heap_limit 之间的空间。</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/07/esp32%E5%B0%81%E8%A3%85lib%E5%BA%93/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/03/07/esp32%E5%B0%81%E8%A3%85lib%E5%BA%93/" class="post-title-link" itemprop="url">esp32封装lib库</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-03-07 16:13:08" itemprop="dateCreated datePublished" datetime="2021-03-07T16:13:08+08:00">2021-03-07</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F/" itemprop="url" rel="index"><span itemprop="name">嵌入式</span></a>
                </span>
            </span>

          
            <span id="/2021/03/07/esp32%E5%B0%81%E8%A3%85lib%E5%BA%93/" class="post-meta-item leancloud_visitors" data-flag-title="esp32封装lib库" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/03/07/esp32%E5%B0%81%E8%A3%85lib%E5%BA%93/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/03/07/esp32%E5%B0%81%E8%A3%85lib%E5%BA%93/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="1-安装开发环境"><a href="#1-安装开发环境" class="headerlink" title="1. 安装开发环境"></a>1. 安装开发环境</h2><p>​    esp32在windows上的环境搭建已经简化了很多，我们可以使用官网的ESP-IDF工具安装器：esp-idf-tools-setup-2.4.exe，它可为您安装所需的交叉编译器、OpenOCD、cmake 和 Ninja 编译工具，以及一款 mconf-idf 配置工具。</p>
<p>​    安装过程中会下载交叉编译工具，cmake等，但是速度会非常慢：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210305205923.png" style="zoom:50%;">

<p>安装工具会把那些工具包下载到.espressif/dist目录下，我们也可以直接自己下载那些工具包，然后拷贝到那个目录下就可以加快esp-idf-tools-setup-2.4.exe的安装。</p>
<p>​                                    <img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210305210729.png" style="zoom:50%;"></p>
<h2 id="2-跑hello-world程序"><a href="#2-跑hello-world程序" class="headerlink" title="2. 跑hello world程序"></a>2. 跑hello world程序</h2><h3 id="2-1-设置目标芯片"><a href="#2-1-设置目标芯片" class="headerlink" title="2.1 设置目标芯片"></a>2.1 设置目标芯片</h3><p>​    在esp-idf-tools-setup-2.4.exe安装过程中，它会自动下载一个esp-idf的文件夹，里面有各种示例程序，我们可以尝试跑一下程序员都喜欢的hello world程序。</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210307133022.png"></p>
<p>打开一个新项目后，应首先设置“目标”芯片 <code>idf.py set-target esp32</code>。注意，此操作将清除并初始化项目之前的编译和配置（如有）。</p>
<h3 id="2-2-编译"><a href="#2-2-编译" class="headerlink" title="2.2 编译"></a>2.2 编译</h3><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210307133607.png" style="zoom:80%;">

<p>​    运行以上命令可以编译应用程序和所有 ESP-IDF 组件，接着生成 bootloader、分区表和应用程序二进制文件。</p>
<h3 id="2-3-烧录"><a href="#2-3-烧录" class="headerlink" title="2.3 烧录"></a>2.3 烧录</h3><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210307134508.png" style="zoom:80%;">

<h3 id="2-4-实验现象"><a href="#2-4-实验现象" class="headerlink" title="2.4 实验现象"></a>2.4 实验现象</h3><p>​    连接板子如下图所示：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210307135046.jpg" style="zoom:10%;">

<p>​    打开串口调试助手，波特率为115200：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210307135447.png" style="zoom:50%;">

<h2 id="3-封装静态lib库范例"><a href="#3-封装静态lib库范例" class="headerlink" title="3. 封装静态lib库范例"></a>3. 封装静态lib库范例</h2><h3 id="3-1-封装lib库"><a href="#3-1-封装lib库" class="headerlink" title="3.1 封装lib库"></a>3.1 封装lib库</h3><p>​    写的待封装的lib库源码如下图所示：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210307145404.png" style="zoom:50%;">

<p>​    为了方便起见，也不去搞什么环境变量了，直接把这些文件拷贝到esp32的交叉编译工具链的目录下，执行下面这些指令开始封装库。</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210307145658.png"></p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210307145815.png"></p>
<h3 id="3-2-写测试程序"><a href="#3-2-写测试程序" class="headerlink" title="3.2 写测试程序"></a>3.2 写测试程序</h3><p>​    测试程序直接在官方的hello_world例程的基础上修改。</p>
<p>​    修改hello_world目录下的CMakeLists.txt文件：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210307153958.png" style="zoom:53%;">

<p>​    添加新的文件夹存放lib库和头文件：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210307154607.png" style="zoom:67%;">

<p>其中新添加的文件夹里面的CMakeLists.txt文件和component.mk文件中的具体内容，可以直接看<a target="_blank" rel="noopener" href="https://gitee.com/xudonglei/esp32_staticlib_test">https://gitee.com/xudonglei/esp32_staticlib_test</a> 中的代码，该代码参考了gitee上开发者<a target="_blank" rel="noopener" href="https://gitee.com/pei_lin_chen">陈沛林</a>的代码，在这里对他表示感谢。</p>
<p>​    我们写的测试程序如下图所示：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210307161147.png" style="zoom:50%;">

<p>​    运行结果也没有问题：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210307161243.png" style="zoom:50%;">
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  


  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/25/"><i class="fa fa-angle-left" aria-label="上一页"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/25/">25</a><span class="page-number current">26</span><a class="page-number" href="/page/27/">27</a><span class="space">&hellip;</span><a class="page-number" href="/page/29/">29</a><a class="extend next" rel="next" href="/page/27/"><i class="fa fa-angle-right" aria-label="下一页"></i></a>
  </nav>



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">xudonglei</p>
  <div class="site-description" itemprop="description">搬砖 嵌入式</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">283</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">67</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">xudonglei</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'BT0Qw9HcTpPv1a6cT1iDBXKz-gzGzoHsz',
      appKey     : 'LMCMh8v02JTMFAwKNRcgoizI',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : true,
      lang       : 'zh-CN' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
